Passed
Push — develop ( 56e412...b602c4 )
by Bjarn
01:15
created

ServiceController   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 111
dl 0
loc 144
rs 10
c 0
b 0
f 0
wmc 27

4 Functions

Rating   Name   Duplication   Size   Complexity  
B controlService 0 28 6
B executeRestart 0 29 7
B executeStart 0 29 7
B executeStop 0 30 7
1
import Dnsmasq from '../services/dnsmasq'
2
import Elasticsearch from '../services/elasticsearch'
3
import Mailhog from '../services/mailhog'
4
import Mariadb from '../services/mariadb'
5
import Mysql from '../services/mysql'
6
import Mysql57 from '../services/mysql57'
7
import Mysql80 from '../services/mysql80'
8
import Nginx from '../services/nginx'
9
import PhpFpm from '../services/phpFpm'
10
import PhpFpm72 from '../services/phpFpm72'
11
import PhpFpm73 from '../services/phpFpm73'
12
import PhpFpm74 from '../services/phpFpm74'
13
import PhpFpm80 from '../services/phpFpm80'
14
import Redis from '../services/redis'
15
import Service from '../services/service'
16
import {getLinkedDatabase} from '../utils/database'
17
import {getLinkedPhpVersion} from '../utils/phpFpm'
18
19
class ServiceController {
20
    allServices: Service[] = [
21
        new Dnsmasq(),
22
        new Elasticsearch(),
23
        new Mailhog(),
24
        new Nginx(),
25
        new Mariadb(),
26
        new Mysql80(),
27
        new Mysql57(),
28
        new PhpFpm80(),
29
        new PhpFpm74(),
30
        new PhpFpm73(),
31
        new PhpFpm72(),
32
        new Redis()
33
    ]
34
35
    executeStart = async (serviceName: string | undefined): Promise<boolean> => {
36
        if (!serviceName) {
37
            for (const service of this.allServices) {
38
                try {
39
                    await this.controlService(service, 'start')
40
                } catch (e) {
41
                    console.log(`Failed to start ${service.service}: ${e.message}`)
42
                }
43
            }
44
            console.log(`Successfully started all Jale services.`)
45
            return true
46
        }
47
48
        for (const service of this.allServices) {
49
            if (service.service === serviceName) {
50
                console.log(`Starting ${service.service}...`)
51
                try {
52
                    await service.start()
53
                    console.log(`Successfully started ${serviceName}.`)
54
                    return true
55
                } catch (e) {
56
                    console.log(`Failed to start ${service.service}: ${e.message}`)
57
                    return false
58
                }
59
            }
60
        }
61
62
        console.warn(`Invalid service: ${serviceName}.`)
63
        return false
64
    }
65
66
    executeStop = async (serviceName: string | undefined): Promise<boolean> => {
67
        if (!serviceName) {
68
            for (const service of this.allServices) {
69
                try {
70
                    await this.controlService(service, 'stop')
71
                } catch (e) {
72
                    console.log(`Failed to stop ${service.service}: ${e.message}`)
73
                }
74
            }
75
76
            console.log(`Successfully stopped all Jale services.`)
77
            return true
78
        }
79
80
        for (const service of this.allServices) {
81
            if (service.service === serviceName) {
82
                console.log(`Stopping ${service.service}...`)
83
                try {
84
                    await service.stop()
85
                    console.log(`Successfully stopped ${serviceName}.`)
86
                    return true
87
                } catch (e) {
88
                    console.log(`Failed to stop ${service.service}: ${e.message}`)
89
                }
90
            }
91
        }
92
93
        console.warn(`Invalid service: ${serviceName}.`)
94
95
        return false
96
    }
97
98
    executeRestart = async (serviceName: string | undefined): Promise<boolean> => {
99
        if (!serviceName) {
100
            for (const service of this.allServices) {
101
                try {
102
                    await this.controlService(service, 'restart')
103
                } catch (e) {
104
                    console.log(`Failed to restarted ${service.service}: ${e.message}`)
105
                }
106
            }
107
            console.log(`Successfully restarted all Jale services.`)
108
            return true
109
        }
110
111
        for (const service of this.allServices) {
112
            if (service.service === serviceName) {
113
                console.log(`Restarting ${service.service}...`)
114
                try {
115
                    await service.restart()
116
                    console.log(`Successfully restarted ${serviceName}.`)
117
                    return true
118
                } catch (e) {
119
                    console.log(`Failed to restarted ${service.service}: ${e.message}`)
120
                    return false
121
                }
122
            }
123
        }
124
125
        console.warn(`Invalid service: ${serviceName}.`)
126
        return false
127
    }
128
129
    /**
130
     * Convenience method to start, stop or restart a service. It also checks if you are restarting PHP or MySQL.
131
     * @param service
132
     * @param action
133
     */
134
    controlService = async (service: Service, action: 'start' | 'stop' | 'restart'): Promise<void> => {
135
        if (service instanceof Mysql) {
136
            const linkedDatabase = await getLinkedDatabase()
137
            if (linkedDatabase.service !== service.service) {
138
                return
139
            }
140
        }
141
142
        if (service instanceof PhpFpm) {
143
            const linkedPhpVersion = await getLinkedPhpVersion()
144
            if (linkedPhpVersion.service !== service.service) {
145
                return
146
            }
147
        }
148
149
        switch (action) {
150
            case 'start':
151
                console.log(`Starting ${service.service}...`)
152
                await service.start()
153
                break
154
            case 'stop':
155
                console.log(`Stopping ${service.service}...`)
156
                await service.stop()
157
                break
158
            case 'restart':
159
                console.log(`Retarting ${service.service}...`)
160
                await service.restart()
161
                break
162
        }
163
    }
164
165
}
166
167
export default ServiceController